﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;

using SilverApp.View;

namespace SilverApp.ViewModel
{
    public class HomeViewModel : ViewModelBase
    {
        private MainPageViewModel                           mparent;
        private UIElement                                   currentPage;
        private ComputerItemViewModel                       currentComputer;
        private FolderItemViewModel                         currentFolder;
        private UIElement                                   friendPage;
        private AccountWindow                               accountWin;
        private ICommand                                    showAccountCommand;

        public HomeViewModel(MainPageViewModel parent)
        {
            mparent = parent;
            friendPage = new FriendsView(new FriendsViewModel(this, mparent.CurrentUser));
            showAccountCommand = new RelayCommand(ShowAccount, CanShowAccount);
            this.RegisterNextView("Computer");
        }


        public UIElement CurrentPage
        {
            get { return currentPage; }
            set
            {
                currentPage = value;
                OnPropertyChanged("CurrentPage");
            }
        }

        public UIElement FriendPage
        {
            get { return friendPage; }
            set
            {
                friendPage= value;
                OnPropertyChanged("FriendPage");
            }
        }

        public FolderItemViewModel CurrentFolder
        {
            get { return this.currentFolder; }
            set
            {
                this.currentFolder = value;
                OnPropertyChanged("CurrentFolder");
                OnPropertyChanged("CurrentFolderName");
            }
        }

        public ComputerItemViewModel CurrentComputer
        {
            get { return this.currentComputer; }
            set
            {
                    this.currentComputer = value;
                  OnPropertyChanged("CurrentComputer");  
                  OnPropertyChanged("CurrentComputerName");
            }
        }

        public String CurrentComputerName
        {
            get { return (this.CurrentComputer != null) ? this.CurrentComputer.ModelName : "no computer"; }
            set
            {
                this.CurrentComputer.ModelName = value;
                OnPropertyChanged("CurrentComputerName");
            }
        }

        public String CurrentFolderName
        {
            get { return (this.CurrentFolder != null) ? this.CurrentFolder.ModelName : "no folder"; }
            set { 
                   this.CurrentFolder.ModelName = value;
                   OnPropertyChanged("CurrentFolderName");
            }
        }

        public LiveService.User CurrentUser
        {
            get { return mparent.CurrentUser; }
        }

        public void RegisterCurrentFolder(FolderItemViewModel cfold)
        {
            CurrentFolder = cfold;
        }

        public void RegisterUser(LiveService.User cuser)
        {
            this.mparent.CurrentUser = cuser;
        }

        public void RegisterCurrentComputer(ComputerItemViewModel ccomp)
        {
            CurrentComputer= ccomp;
        }

        public void RegisterNextView(String next)
        {
            try
            {
                switch (next)
                {

                    case "Computer":
                        CurrentPage = new ComputerView(new ComputerViewModel(this));
                        break;

                    case "Folder":
                        CurrentPage = new FolderView(new FolderViewModel(this));
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public ICommand ShowAccountCommand
        {
            get { return this.showAccountCommand; }
            set { this.showAccountCommand = value; }
        }

        public AccountWindow AccountWin
        {
            get { return this.accountWin; }
            set { this.accountWin = value; }
        }

        public void ShowAccount(object parameter)
        {
            try
            {
                this.accountWin = new AccountWindow(new AccountViewModel(this));
                this.accountWin.Show();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public bool CanShowAccount(object parameter)
        {
            return true;
        }
    }
}
